package org.zl.controller;

import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zl.pojo.ClazzInfo;
import org.zl.pojo.MajorInfo;
import org.zl.pojo.Undergraduate;
import org.zl.service.ClazzInfoService;
import org.zl.service.MajorInfoService;
import org.zl.service.UndergraduateService;
import org.zl.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/all")
public class UndergraduateController {



    @Autowired
    private HttpServletRequest request;


    @Autowired
    private MajorInfoService majorInfoService;

    @Autowired
    private UndergraduateService undergraduateService;

    @RequestMapping("/s-list.action")
    public String List() {

        List<Undergraduate> students = undergraduateService.getStudentByClazz();
        System.out.println("student:"+students);
        request.setAttribute("students",students);
        return "ClazzMajor/show_allList";
    }


    @RequestMapping("/all-list.action")
    public String allList(Integer majId, Integer page, Integer pageSize) {
        System.out.println("zbuzbu");
        int count = undergraduateService.getStudentCountByClazz(majId);
        System.out.println(count);
        if(pageSize == null) {
            pageSize = 10;
        }
        int allPages =(count +pageSize-1)/pageSize;

        if(page==null || page<0)
            page = 1;
        else if(page>allPages)
            page = allPages;

        List<Undergraduate> students = undergraduateService.getStudentsByClazz4Page(majId, page, pageSize);
        System.out.println("student:"+students);
        request.setAttribute("students", students);

        List<MajorInfo> majorInfos=majorInfoService.getAllMajores();
        System.out.println(majorInfos);
        List<Undergraduate> users = undergraduateService.getStudentByClazz();
        request.setAttribute("users",users);
        request.setAttribute("majorInfos", majorInfos);
        request.setAttribute("page", page);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("majId", majId);
        request.setAttribute("count", count);
        request.setAttribute("allPages", allPages);

        return "ClazzMajor/show_allList";

    }
}
